package pacman;

import java.awt.Color;
import java.util.Random;
import java.util.ArrayList;

/**
 * This is the implementation of a red ghost, a ghost who hunts very efficiently the nearest pacman on the game. He has so much anger against pacmen that he often gets on the wrong way because he is anger-blind.
 */
public class cRedGhost implements iGhost {
    private Position pos;
    private Position lastPos;
    private cCore parent;
    private Random rand = new Random();
    private Color color = new Color(255, 50, 50);
    
    /**
     * Creates a new instance of cRedGhost.
     * @param parent The core where the ghost is to be created, i.e. the core of the game.
     * @param pos The position where the ghost is to be created.
     */
    public cRedGhost(cCore parent, Position pos) {
        this.parent = parent;
        this.pos = pos;
        this.lastPos = pos;
    }

    /**
     * Makes the red ghost to move
     */
    public void Move() {
        cPath temp = null;
        cPath path = null;
        for(int i = 0; i < parent.getPacmanList().size(); i++) {
            if(!parent.getPacmanList().get(i).IsDead()) {
                temp = new cPath(this.pos, parent.getPacmanList().get(i).getPosition(), parent, this);
                if(path == null || path.getLength() == -1 || temp.getLength() >= 0 && path.getLength() > temp.getLength()) {
                path = temp; 
                }
            }
        }
        
        if((path != null && path.getLength() == -1) || rand.nextInt(3) > 1 || (path != null && path.getNextStep().equals(new Position(-1, -1)))) {
            lastPos = pos;
            pos = randomPos();
        }
        
        else {
            lastPos = pos;
            pos = path.getNextStep();
        }
    }
    
    /**
     * Returns the current position of this ghost.
     * @return The current positon of this ghost.
     */
    public Position getPosition() {
        return pos;
    }

    /**
     * Returns the name of the image to be loaded for this ghost.
     * @return A string containing the name of the image to be loaded for this ghost.
     */
    public String getImageName() {
        return "redghost";
    }
    
    private Position randomPos() {
        ArrayList<Position> nextPos = new ArrayList<Position>();
        Position tmp = this.pos.clone();
        
        //Tests all positions around current one to see if it's usable
        tmp.incX(1);
        if(parent.isAccessible(tmp,this) && !tmp.equals(lastPos)) {
            nextPos.add(tmp.clone());
        }
        tmp.incX(-2);
        if(parent.isAccessible(tmp,this) && !tmp.equals(lastPos)) {
            nextPos.add(tmp.clone());
        }
        tmp.incX(1);
        tmp.incY(1);
        if(parent.isAccessible(tmp,this) && !tmp.equals(lastPos)) {
            nextPos.add(tmp.clone());
        }
        tmp.incY(-2);
        if(parent.isAccessible(tmp,this) && !tmp.equals(lastPos)) {
            nextPos.add(tmp.clone());
        }
        
        if(nextPos.size()<1) {
            nextPos.add(lastPos);
        }
        
        return nextPos.get(rand.nextInt(nextPos.size()));
    }
}